<script setup>
import { ref,computed,Transition,KeepAlive, watch,onMounted,nextTick,onUnmounted  } from 'vue';
import ScrollBar from '@/components/ScrollBar/index.vue';
import TopBar from '@/components/Layout/topBar.vue'
import { useUserInfoStore } from '@/stores/userInfo.js';

import { useRoute,useRouter } from 'vue-router';
import { RouterView } from 'vue-router';
import Menu from '@/components/Layout/menu.vue'
import { clearAllCache } from '@/utils/globalWay.js'; 
import { useTagsStore } from '@/stores/tag.js'
import { objectArraysAreEqual } from '@/utils/common.js'

let tagsStore  = useTagsStore();

let isCollapse =  ref(false);
let textShow = ref(true);
let menuRef = ref(null);
let route = useRoute();
let router = useRouter();

let UserInfoStore = useUserInfoStore();
const excludeArr = ref([]);
const timer = ref('');
watch(route,()=>{
    getInclude();
})
const getInclude = ()=>{
    excludeArr.value = tagsStore.excludeArr;
    nextTick(()=>{
        excludeArr.value = []; 
        tagsStore.setExcludeArr(excludeArr.value);
    }) 
}

let isCloseTag = computed(()=>{
  return tagsStore.isCloseTag
})
watch(isCloseTag,(newval)=>{
  tagsStore.setExcludeArr([...tagsStore.excludeArr,route.name]);
  tagsStore.removeTags(route);

  if(route && (route.meta.hidden === true)){

    tagsStore.changeIsRefresh(true);
  }
})
function openClose() {
  isCollapse.value = !isCollapse.value;
  changeCollapse(isCollapse);
};
function changeCollapse(val) {
  if(isCollapse.value){
    $('.slide-bar').animate({width:'80px'})
    $('.main').animate({width:$('.layout').width() - 80})
    textShow.value = false;
  }else{
    $('.slide-bar').animate({width:'200px'},()=>{
      textShow.value = true;
    })
    $('.main').animate({width:$('.layout').width() - 200})
  }

};
function handleCommand(name){
  if(name == 'loginOut'){
    clearAllCache();
    router.push('/login')
  }else if(name == 'changepassword'){
  }else if(name == "customerservice"){
  }
};
function removeTag(value){

    tagsStore.setExcludeArr([value.name]);
    tagsStore.removeTags(value);
    let lastTag = tagsStore.tags[tagsStore.tags.length-1];
    router.replace(lastTag.fullPath);

}
const secondRouterList = ref([]);
//获取二级路由
const getSecondRouter = (routerList)=>{
  let oldSecondRouterList = [...secondRouterList.value];
  if(!objectArraysAreEqual(routerList, oldSecondRouterList)){
    secondRouterList.value = [];
    nextTick(()=>{
      secondRouterList.value = routerList
    })
  }


};
const maskerFlag = ref(true);
onMounted(()=>{
		$('.door .nr').animate({opacity:0},1000,()=>{
      maskerFlag.value = false;
    });
    sessionStorage.removeItem('closeTagName');
})
onUnmounted(()=>{
  clearInterval(timer.value);
})
</script>

<template>
  <div class="door" v-if="maskerFlag">
		<div class="nr"></div>
	</div>
  <div class="layout">
    <TopBar @getSecondRouter="getSecondRouter"></TopBar>
    <div class="layout-content">
      <div
        class="slide-bar"
        id="slidebar"
        v-if="secondRouterList && secondRouterList.length>0"
      >

        <div class="menu" ref="menuRef">
          <Menu :isCollapse="isCollapse" :secondRouterList="secondRouterList"></Menu>
        </div>
      </div>
      
      <div
        class="main"
        :style="{width:secondRouterList && secondRouterList.length>0?'calc(100% - 180px)':'100%'}"
      >

        <div class="nav-bar" v-if="route.name == 'Hello'?false:true">
          <ScrollBar @removeTag="removeTag"></ScrollBar>
        </div>

        <div class="main-body" :style="{padding:route.name == 'Hello'?'0px':'12px'}">

            <div class="nr" >

                <RouterView #="{ Component  }" >
                    <keep-alive   :exclude="excludeArr">
                        
                        <component :is="Component" />
                    </keep-alive>
                </RouterView>
              
            </div>


        </div>
      </div>
    </div>

  </div>
</template>
<style lang="less" scoped>


.layout {
  width: 100%;
  height: 100%;
  background: #F3F3F3;
  .layout-content{
    width: 100%;
    height: calc(100% - 48px);
    display: flex;

    justify-content: flex-start;
  }
  .rotatemenu{
    transform: rotate(180deg);
  }
  .slide-bar {
    width: 180px;
    box-sizing: border-box;
    padding: 0 8px;
    height: 100%;
    display: flex;
    flex-direction: column;
    background: #fff;
    --el-menu-bg-color: rgba(0, 0, 0, 0);
    --el-menu-text-color: #000000;
    --el-menu-item-height: 32px;
    --el-menu-sub-item-height:32px;
    --el-menu-level-padding: 24px;
    --el-menu-base-level-padding: 8px;
    :deep(.el-menu-item){
      margin-bottom: 10px;
      border-radius: 20px 4px 4px 20px;
    }
    :deep(.el-sub-menu__title){
      margin-bottom: 10px;
      span{
        font-weight: bold;
        font-family:"Microsoft YaHei";
      }
    }

    .menu {
      width: 100%;
      height: 100%;

      padding:16px 0px;
      box-sizing: border-box;
      overflow: hidden;
      :deep(.el-menu){
        border-right:0;
      }
      :deep(.el-sub-menu__icon-arrow){
        right:8px;
        svg{
          display: none;
        }
        &::before{
          content:"";
          background: url('@/assets/images/jt-down.png');
          width: 16px;
          height: 16px;
          background-size: 100% 100%;
        }
      }
      :deep(.el-menu-item.is-active){
        background:#3480FF;
 
        color:#fff;
        li{
          &:hover{
            background:#3480FF;

            color:#fff;
          }
        }

      }
      :deep(.el-sub-menu){
        background: rgba(0,0,0,0);
      }
      &:hover {
        overflow: auto;
      }

    }
    .logo-info {
      width: 100%;
      height: 40px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      box-sizing: border-box;
      padding: 0 20px;
      border-bottom:1px solid rgba(255, 255, 255, 0.2);
      .home-text{
        width: 81px;
        height: 20px;
        cursor: pointer;
      }
      img {
        cursor: pointer;
      }
      .left-logo {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        img {
          width: 105px;
          height: 22px;
          margin-right: 10px;
        }
        span {
          font-size: 14px;
          font-weight: 400;
          color: #303133;
        }
      }
    }
  }

  .nav-bar {
    height: 32px;
    width: 100%;
    border-top: 1px solid #ebeef5;
    box-sizing: border-box;
    overflow: auto;
    display: flex;
    align-items: flex-end;
  }


  .main {
    height: 100%;
    display: flex;
    flex-direction: column;
    width: calc(100% - 180px);
    .main-body {
      height: 100%;
      width: 100%;
      box-sizing: border-box;
      padding: 12px;
      overflow: auto;
      .nr{
        width: 100%;
        height: 100%;
        overflow: auto;
        background: #fff;
        box-sizing: border-box;
        border-radius: 4px;
      }
    }
  }
}
.show-width {
  width: calc(100% - 200px);
}
.hide-width {
  width: calc(100% - 80px);
}
/* fade-transform */
.fade-transform-leave-active,
.fade-transform-enter-active {
  transition: all 0.5s;
}

.fade-transform-enter {
  opacity: 1;

}

.fade-transform-leave-to {
  opacity: 0.3;

}

</style>
<style lang="less" scoped>
.door{
  width: 100%;
  height: 100%;
  overflow:hidden;
  position: fixed;
  z-index: 100;
  .nr{
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100%;
    height: 100%;
    opacity: 1;
    transform: translate(-50%, -50%);
    background: rgba(255,255,255,1);
  }

}


</style>
